package cn.caskm.souvenirshopsystem.controller.admin;

import cn.caskm.souvenirshopsystem.domain.Result;
import cn.caskm.souvenirshopsystem.domain.dto.LoginFormDTO;
import cn.caskm.souvenirshopsystem.domain.dto.PageDTO;
import cn.caskm.souvenirshopsystem.domain.dto.UpdatePasswordFormDTO;
import cn.caskm.souvenirshopsystem.domain.po.AdminUser;
import cn.caskm.souvenirshopsystem.domain.query.AdminUserListQuery;
import cn.caskm.souvenirshopsystem.domain.vo.AdminUserVO;
import cn.caskm.souvenirshopsystem.service.AdminUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
@RequiredArgsConstructor
public class AdminUserController {

    private final AdminUserService adminUserService;

    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginFormDTO loginFormDTO) {
        return adminUserService.login(loginFormDTO.getUserName(), loginFormDTO.getPassword());
    }

    @GetMapping("/adminList")
    public Result<PageDTO<AdminUserVO>> adminList(AdminUserListQuery query) {
        return adminUserService.adminList(query);
    }

    @GetMapping("/detail/{id}")
    public Result<AdminUserVO> detail(@PathVariable("id") Long id) {
        return adminUserService.detail(id);
    }

    @PostMapping("/saveOrUpdate")
    public Result<Object> saveOrUpdate(@RequestBody AdminUser user) {
        adminUserService.saveOrUpdateUser(user);
        return Result.success();
    }

    @DeleteMapping("/deleteById/{id}")
    public Result<Object> deleteById(@PathVariable("id") Long id) {
        adminUserService.removeById(id);
        return Result.success();
    }

    @PostMapping("/logout")
    public Result<Object> logout() {
        // 无需实现，前端把token干掉就行了
        return Result.success();
    }

    @GetMapping("/getLoginUserInfo")
    public Result<AdminUserVO> getLoginUserInfo() {
        return adminUserService.getLoginUserInfo();
    }

    @PostMapping("/updatePassword")
    public Result<Object> updatePassword(@RequestBody @Validated UpdatePasswordFormDTO body) {
        adminUserService.updatePassword(body);
        return Result.success();
    }

}
